/*
 * Copyright (C) 2013 The original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy
 * of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.zapta.apps.maniana.editors;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import com.zapta.apps.maniana.R;
import com.zapta.apps.maniana.annotations.MainActivityScope;
import com.zapta.apps.maniana.editors.EditRepeat;
import com.zapta.apps.maniana.editors.EditRepeatDay;
import com.zapta.apps.maniana.editors.EditRepeatMonth;
import com.zapta.apps.maniana.editors.EditRepeatWeek;
import com.zapta.apps.maniana.editors.EditRepeatYear;
import com.zapta.apps.maniana.model.ItemColor;
import com.zapta.apps.maniana.model.ItemRepeatType;

/*
**
** @author Elliott Mitchell
**/
@MainActivityScope
public class ItemTextEditorDueExtras extends ItemTextEditorExtra
implements View.OnClickListener {

	/** Stores the repeat editor currently being used */
	private EditRepeat mRepeat;

	@Override
	public View onCreateView(LayoutInflater inflator, ViewGroup container,
Bundle savedInstanceState) {
		super.onCreateView(inflator, container, savedInstanceState);
		// API 11+ layout obscures the button text on earlier devices
		final View view=inflator.inflate(R.layout.editor_due_extras,
container, false);

		// setup the initial value
                TextView showEarly=(TextView)view.findViewById(R.id.editor_show_early);
                showEarly.setText(Byte.toString(getArguments().
getByte("item_show_early", (byte)0)));

		Button b;
		b=(Button)view.findViewById(R.id.editor_days_incr);
		b.setOnClickListener(this);
		b=(Button)view.findViewById(R.id.editor_days_decr);
		b.setOnClickListener(this);

		// setup the color spinners
		Spinner spin;
		ItemColor color;

		spin=(Spinner)view.findViewById(R.id.color_due);
		spin.setAdapter(mColors);
		spin.setOnItemSelectedListener(this);
		if(getArguments().containsKey("item_color")) {
			color=ItemColor.fromKey(getArguments().
getString("item_color"), null);
			spin.setSelection(color.getIndex());
		} else color=ItemColor.NONE;

		spin=(Spinner)view.findViewById(R.id.color_early);
		spin.setAdapter(mColors);
		spin.setOnItemSelectedListener(this);
		if(getArguments().containsKey("item_color_early")) {
			color=ItemColor.fromKey(getArguments().
getString("item_color_early"), null);
			spin.setSelection(color.getIndex());
		}
		spin.setSelection(color.getIndex());

		spin=(Spinner)view.findViewById(R.id.color_overdue);
		spin.setAdapter(mColors);
		spin.setOnItemSelectedListener(this);
		if(getArguments().containsKey("item_color_late")) {
			color=ItemColor.fromKey(getArguments().
getString("item_color_late"), null);
			spin.setSelection(color.getIndex());
		}
		spin.setSelection(color.getIndex());

		// setup the repeat type spinner
		spin=(Spinner)view.findViewById(R.id.editor_repeat_type);
		ArrayAdapter<CharSequence> repeat_options=ArrayAdapter.
createFromResource(getActivity(), R.array.editor_repeat_options,
android.R.layout.simple_spinner_item);
		repeat_options.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spin.setAdapter(repeat_options);
		spin.setOnItemSelectedListener(this);

		String repeatStr=getArguments().getString("item_repeat_type");
		ItemRepeatType repeatType=ItemRepeatType.fromKey(repeatStr);
		int idx;
		switch(repeatType) {
		case DAY:
			idx=(getArguments().getShort("item_repeat_days",
(short)0)&0xFFFF)%7!=6?1:2;
		break;
		case MONTH:
			idx=(getArguments().getByte("item_repeat_months",
(byte)0)&0xFF)%12!=11?3:4;
		break;
		case NONE:
		default:
			idx=0;
                }

		spin.setSelection(idx);

		return view;
	}

	@Override
	public void onClick(View view) {
		switch(view.getId()) {
		case R.id.editor_days_incr:
		case R.id.editor_days_decr:
			buttonShowEarly(view);
		break;
		}
	}

	/** Called for the before due date setting */
	public void buttonShowEarly(View view) {
		TextView showEarly=(TextView)getView().findViewById(R.id.editor_show_early);
		byte val;
		val=getArguments().getByte("item_show_early", (byte)0);

		val+=view.getId()==R.id.editor_days_incr?+1:-1;
		if(val<0) val=0;
		getArguments().putByte("item_show_early", val);

		showEarly.setText(Byte.toString(val));
	}


	/** Called for the repeat type selector being selected */
	public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
		final int parId=parent.getId();

		switch(parId) {
		case R.id.color_early:
			setColor(pos, "item_color_early");
		break;
		case R.id.color_due:
			setColor(pos, "item_color");
		break;
		case R.id.color_overdue:
			setColor(pos, "item_color_late");
		break;
		case R.id.editor_repeat_type:
			setRepeat(pos);
		break;
		}
	}

	private void setRepeat(int pos) {
		final EditRepeat er;

		switch(pos) {
		case 0:
			er = null;
			getArguments().remove("item_repeat_type");
		break;
		case 1:
			er = new EditRepeatDay();
		break;
		case 2:
			er = new EditRepeatWeek();
		break;
		case 3:
			er = new EditRepeatMonth();
		break;
		case 4:
			er = new EditRepeatYear();
		break;
		default:
			// shouldn't ever occur
			return;
		}

		FragmentTransaction ft = getChildFragmentManager().beginTransaction();
		if(er != null) {
			er.setArguments(getArguments());
			ft.replace(R.id.editor_repeat_frag, er);
		} else if(mRepeat != null) ft.remove(mRepeat);
		ft.commit();
		mRepeat = er;
	}

	private void setColor(int pos, String type) {
		getArguments().putString(type, ItemColor.values()[pos].getKey());
	}

	int getSpinnerTextId(int id) {
		switch(id) {
		case R.id.color_early:
			return R.string.editor_early;
		case R.id.color_due:
			return R.string.editor_due;
		case R.id.color_overdue:
			return R.string.editor_overdue;
		}
		return 0;
	}
}

